import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { RedisService } from './redis/redis.service';
import { ExcludeAuth } from './auth/decorators/exclude-auth.decorator';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService, private readonly redisService: RedisService) { }

  @Get()
  @ExcludeAuth()
  async getHello(): Promise<string> {
    // 判断是不是又缓存，没有再设置，有就拿值
    // 尝试从缓存中获取数据
    const cachedValue = await this.redisService.get('hello');
    if (cachedValue) {
      return cachedValue;
    }

    // 缓存中没有数据,从服务中获取并缓存
    const freshValue = await this.appService.getHello();
    await this.redisService.set('hello', freshValue);
    return freshValue;
  }
}
